package com.amikomgamedev.ramashinta.state;

import org.andengine.entity.modifier.MoveXModifier;
import org.andengine.entity.scene.ITouchArea;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.scene.background.Background;
import org.andengine.input.touch.TouchEvent;
import org.andengine.input.touch.detector.ClickDetector;
import org.andengine.input.touch.detector.ScrollDetector;
import org.andengine.util.color.Color;
import android.view.KeyEvent;

import com.amikomgamedev.ramashinta.Utils;
import com.amikomgamedev.ramashinta.graphic.Graphic_Path;

public class State_Menu_Stage extends State_Menu_Level implements Graphic_Path {

	private final float	DURATION	= 0.3f;

	private	float[] minX	= new float[5];
	private	float[] centerSprXStage	= new float[5];
	private	float[] maxX	= new float[5];
	public	float[] stageX  = new float[5];
	
	public boolean geser = false;
	public int geserKanan = 0;
	public int geserKiri = 1;
	
	protected void create_Menu_Stage()
	{	
		int a= 0;
		
		for(int i=0; i< stageX.length; i++){
			
			stageX[i] = 0;
			stageX[i] += a;
			
			a += RES_WIDTH;
		}
		graphic.attachMenuSelectStage();
		graphic.attachMenuStageLingkaran();
		graphic.attachArrowStage();
		
		m_Scene.setBackground(new Background(Color.BLACK));
		isCameraMove = false;
		
//		graphic.attachStage();
	}

	


	@Override
	public void onScroll(ScrollDetector pScollDetector, int pPointerID,
			float pDistanceX, float pDistanceY) {
		if (State_Menu_Current == STATE_MENU_STAGE)
		{
			for (int i = 0; i < MENU_SELECT_STAGE_BG.length; i++)
			{
				graphic.spr_Menu_Select_Stage[i].setPosition(graphic.spr_Menu_Select_Stage[i].getX() + pDistanceX,
						graphic.spr_Menu_Select_Stage[i].getY());
			}

		}
		super.onScroll(pScollDetector, pPointerID, pDistanceX, pDistanceY);
	}

	@Override
	public void onScrollFinished(ScrollDetector pScollDetector, int pPointerID,
			float pDistanceX, float pDistanceY) {
		if (State_Menu_Current == STATE_MENU_STAGE)
		{
			
		    	for (int i = 0; i < graphic.spr_Menu_Select_Stage.length; i++){
				minX[i]	= graphic.spr_Menu_Select_Stage[i].getX();
				maxX[i]	= graphic.spr_Menu_Select_Stage[i].getX() + graphic.spr_Menu_Select_Stage[i].getWidth();
				centerSprXStage[i]	= graphic.spr_Menu_Select_Stage[i].getX()+ (graphic.spr_Menu_Select_Stage[i].getWidth())/2;
		    	}
			    if(!geser){
		    	for (int j = 0; j < graphic.spr_Menu_Select_Stage.length; j++){
		    			if((centerSprXStage[j] > 0) && (centerSprXStage[j] < m_Camera.getCenterX()) && (j != (graphic.spr_Menu_Select_Stage.length-1) )){
		    				geser(geserKiri);
		    			}else if((centerSprXStage[j] > m_Camera.getCenterX()) && (centerSprXStage[j] < m_Camera.getXMax()) && j != 0){
		    				geser(geserKanan);
		    			}else if(j == 0 && minX[j] > 0){
		    				float[] pToX	= {0,RES_WIDTH, RES_WIDTH * 2, RES_WIDTH * 3,RES_WIDTH * 4};
		    				stageX = pToX;
		    				for (int i = 0; i < 5; i++){ 
		    						graphic.spr_Menu_Select_Stage[i].registerEntityModifier(
		    						new MoveXModifier(DURATION, minX[i], pToX[i]));			    
		    				}
		    			}else if(j== 4 && maxX[j] < m_Camera.getXMax()){
		    				float[] pToX	= {-(RES_WIDTH*4),-(RES_WIDTH*3),-(RES_WIDTH*2),-RES_WIDTH, 0};
		    				stageX = pToX;
		    				for (int i = 0; i < 5; i++){ 
		    						graphic.spr_Menu_Select_Stage[i].registerEntityModifier(
		    						new MoveXModifier(DURATION, minX[i], pToX[i]));			    
		    				}}
		    			
		    	}}
			}
		
		super.onScrollFinished(pScollDetector, pPointerID, pDistanceX, pDistanceY);
	}


	public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent) {
		if (State_Menu_Current == STATE_MENU_STAGE)
		{
		
			m_ClickDetector.onTouchEvent(pSceneTouchEvent);
			m_Scroll.onTouchEvent(pSceneTouchEvent);
			
		}
		return super.onSceneTouchEvent(pScene, pSceneTouchEvent);
	}


	@Override
	public boolean onKeyUp(int keyCode, KeyEvent event) {
		if (State_Menu_Current == STATE_MENU_STAGE)
		{
			if (keyCode == KeyEvent.KEYCODE_BACK)
			{
				System.exit(0);
			}
		}
		return super.onKeyUp(keyCode, event);
	}
	
	public void geser(Integer geserarah){
		geser = true;
		if(geserarah == geserKiri){
			for (int i = 0; i < 5; i++)
			{	
				stageX[i] -= RES_WIDTH; 
				graphic.spr_Menu_Select_Stage[i].registerEntityModifier(
					new MoveXModifier(DURATION, minX[i], stageX[i]));	
				Utils.TRACE("STAGE Ke" + i + "  KorX: " + stageX[i]);
			}
		}else if(geserarah == geserKanan){
			for (int i = 0; i < 5; i++)
			{
			stageX[i] += RES_WIDTH; 
			graphic.spr_Menu_Select_Stage[i].registerEntityModifier(
					new MoveXModifier(DURATION, minX[i], stageX[i]));			    
		}}
		geser = false;
	}




	@Override
	public void onUpdate(float pSecondsElapsed) {
		if (State_Menu_Current == STATE_MENU_STAGE)
		{
		for(int i=0; i<graphic.spr_Menu_Select_Stage.length; i++){
		if(graphic.spr_Menu_Select_Stage[i].getX() == 0){
			if(graphic.lst_Menu_Stage_Lingkaran.get(i).getCurrentTileIndex() != 0){
				graphic.lst_Menu_Stage_Lingkaran.get(i).setCurrentTileIndex(0);
			}
			
			if(i == 0){
				graphic.spr_Stage_Arrow[0].setVisible(false);
			}else if(i == 4){
				graphic.spr_Stage_Arrow[1].setVisible(false);
			}else{
				graphic.spr_Stage_Arrow[0].setVisible(true);
				graphic.spr_Stage_Arrow[1].setVisible(true);
			}
		}else{
			if(graphic.lst_Menu_Stage_Lingkaran.get(i).getCurrentTileIndex() != 1){
				graphic.lst_Menu_Stage_Lingkaran.get(i).setCurrentTileIndex(1);}
		}}
		
		}
		super.onUpdate(pSecondsElapsed);
	}




	@Override
	public void onClick(ClickDetector pClickDetector, int pPointerID,
			float pSceneX, float pSceneY) {
		
		if (State_Menu_Current == STATE_MENU_STAGE)
		{
			
			if(Utils.isOnArea(pSceneX, pSceneY, graphic.spr_Stage_Arrow[0]) && graphic.spr_Stage_Arrow[0].isVisible()){
				Utils.TRACE("kanan");
			}else if(Utils.isOnArea(pSceneX, pSceneY, graphic.spr_Stage_Arrow[1]) && graphic.spr_Stage_Arrow[1].isVisible()){
				geser(geserKiri);
			}else{
			graphic.detachMenuStage();
			switchState(STATE_MENU_LEVEL);
		}}
		
		
		super.onClick(pClickDetector, pPointerID, pSceneX, pSceneY);
	}




	
	
	
	
}
